const app = getApp()
var webhost = app.globalData.webhost;
var getInstance;
var insClick;
import Notify from '../Vant/notify/notify';

Page({
  data: {
    task: {},
    plan: {},
    radio: '',
    costList: [],
    show: false,
    money: '',
    cost: {},
    hotLine: [],
  },

  onClose(event) {
    if (event.detail === 'confirm') {
      setTimeout(() => {
        this.setData({
          show: false
        });
      }, 1500);
      this.update();
    } else {
      this.setData({
        show: false
      });
    }
  },

  update(){
    var that = this;
    wx.showNavigationBarLoading();
    console.log(that.data.cost);
    var data = [{
      String: {
        driverAccount: app.globalData.driverAccount,
        sessionid: app.globalData.sessionid,
        costId: that.data.cost.costId,
        n1: that.data.money
      }
    }]
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'updateFee', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(data)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      complete() {
        wx.hideNavigationBarLoading();
        that.setData({ money: '' });
      },
      success({ data }) {
        if (data.code && data.code == '000000') {
          wx.showToast({ title: '提交成功！' })
          getInstance();
          that.setData({
            cost: {}
          })
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络连接失败',
          icon: 'none'
        })
      }
    })
  },
  
  inputChange(e){
    this.setData({ money: e.detail })
  },

  picClick(){
    wx.navigateTo({
      url: './img?lt=task',
    }) 
  },

  service(){
    var length = this.data.hotLine.length;
    if(length == 1){
      wx.makePhoneCall({
        phoneNumber: this.data.hotLine[0].codeName,
      })
    } else if (length == 0){
      wx.showToast({
        title: '暂无客服电话',
      })
    } else if (length > 1){
      var list = new Array;
      for(var i =0;i < length; i++){
        list[i] = this.data.hotLine[i].codeName
      }
      wx.showActionSheet({
        itemList: list,
        success: function(res){
          wx.makePhoneCall({
            phoneNumber: list[res.tapIndex],
          })
        }
      })
    }
    
  },

  call(e) {
    var phone = e.currentTarget.dataset.phone;
    if (phone) {
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }
  },

  change(e) {
    var id = +e.currentTarget.dataset.index;
    this.setData({
      radio: id
    })
    console.log(this.data.radio);
  },

  updateFee(){
    var that = this;
    console.log(that.data.radio === '');
    console.log(that.data.radio);
    if (that.data.radio === '') {
      Notify({
        text: '请先选择一项费用信息！',
        duration: 1200
      })
      return false;
    } 
    var cost = that.data.costList[that.data.radio];
    if (cost.n2 != '已确认'){
      Notify({
        text: '数量未确认！',
        duration: 1200
      })
      return false;
    }
    that.setData({
      cost: cost,
      show: true
    })
  },

  setClick(e) {
    var that = this;
    var taskid = e.currentTarget.dataset.id;
    var taskCode = e.currentTarget.dataset.taskcode
    var actionCode = e.currentTarget.dataset.actioncode;
    insClick(app.globalData.planid, taskCode, taskid, actionCode);
  },

// 报销
  reimClick(){
    wx.navigateTo({
      url: '../func/feeApplication?task=1',
    })
  },

  // 异常
  absClick() {
    wx.navigateTo({
      url: '../func/doException?task=1',
    })
  },



  onLoad: function(options) {
    var that = this;

    that.setData({
      plan: app.globalData.plan
    })

    //获取客服热线电话  getCodeList
    var codeList = [{
      String: {
        codeListKey: 'tsp2_hotLine'
      }
    }];
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'getCodeList', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(codeList)

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        if (res.data.code && res.data.code == '000000') {
          that.setData({
            hotLine: res.data.codeList,
          })
        }
      }
    })

    getInstance = function () {
      wx.showNavigationBarLoading();
      var data = [{
        String: {
          driverAccount: app.globalData.driverAccount,
          sessionid: app.globalData.sessionid,
          taskid: app.globalData.taskid
        }
      }]
      wx.request({
        url: webhost,
        data: {
          eap_username: "driver",
          eap_password: "Driver2018",
          boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
          methodName: 'queryPlanTask', //调用的具体方法
          returnType: "json", //返回参数类型
          parameters: JSON.stringify(data)
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        complete() {
          wx.hideNavigationBarLoading();
        },
        success({ data }) {
          console.log(data)
          if (data.code && data.code == '000000') {
            var item = data.data;
              if(item.actionList){
                for (var j = 0; j < item.actionList.length; j++) {
                  switch (item.actionList[j].actionCode) {
                    case 'AC10':
                      item.start = item.actionList[j];
                      break;
                    case 'AC20':
                      item.build = item.actionList[j];
                      break;
                    case 'AC30':
                      item.end = item.actionList[j];
                      break;
                    case 'AC40':
                      item.stop = item.actionList[j];
                      break;
                    case 'AC50':
                      item.return = item.actionList[j];
                      break;
                    case 'EXCE':
                      item.abnormal = item.actionList[j];
                      break;
                    case 'COST':
                      item.cost = item.actionList[j];
                      break;
                    case 'SNAP':
                      item.snap = item.actionList[j];
                      break;
                  }
              }
            }
            that.setData({
              task: item
            })
            if(item.costList){
              var old = item.costList;
              console.log(old);
              var costList = new Array();
              for (var n = 0; n < old.length; n++){
                console.log(n)
                var item2 = old[n];
                var a = (+old[n].costAmount).toFixed(2);
                var b ;
                if (old[n].n3){
                  b = (+old[n].n3).toFixed(2);
                  console.log(b)
                }else{
                  b = ''
                }
                item2.costAmount = a;
                item2.n3 = b;
                costList.push(item2);
              }
              console.log(costList);              
              that.setData({
                costList: costList
              })
            }
          } else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
        },
        fail: function () {
          wx.showToast({
            title: '网络连接失败',
            icon: 'none'
          })
        }
      })
    }

    insClick = function (planid, taskCode, taskid, actionCode) {
      wx.showNavigationBarLoading();
      var data = [{
        String: {
          driverAccount: app.globalData.driverAccount,
          sessionid: app.globalData.sessionid,
          planId: planid,
          taskid: taskid,
          taskCode: taskCode,
          actionCode: actionCode,
          remarks: ''
        }
      }]
      wx.request({
        url: webhost,
        data: {
          eap_username: "driver",
          eap_password: "Driver2018",
          boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
          methodName: 'doGeneralEvent', //调用的具体方法
          returnType: "json", //返回参数类型
          parameters: JSON.stringify(data)
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        complete() {
          wx.hideNavigationBarLoading();
        },
        success({ data }) {
          if (data.code && data.code == '000000') {
            wx.showToast({
              title: '操作成功',
              icon: 'success'
            })
            getInstance();
          } else {
            wx.showModal({
              title: '操作异常',
              showCancel: false,
              confirmColor: '#118EDE',
              content: data.msg,
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '网络连接失败',
            icon: 'none'
          })
        }
      })
    }

  },

  onShow: function() {
    var that = this;

    getInstance();    
  },

  onPullDownRefresh: function() {

  },

  onReachBottom: function() {

  },

  onShareAppMessage: function() {

  }
})